import React, { Component } from "react";
// 一切皆组件
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>首页</div>;
  }
}
class About extends Component {
  render() {
    return <div>关于页</div>;
  }
}
class Mine extends Component {
  fn = () => {
    const { history } = this.props;
    history.push("/mine/567");
  };

  render() {
    // ?. 可选链操作符
    // fn?.()
    // console.log(this.props.match?.params?.id);
    return (
      <div>
        我的页 - {this.props.match?.params?.id} -{" "}
        <button onClick={this.fn}>btn</button>
      </div>
    );
  }
}

class Basic extends Component {
  state = {};
  render() {
    return (
      <>
        <h3>动态路由</h3>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine/234">mine</Link>
          </li>
        </ul>

        <Switch>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine/:id" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default Basic;
